// DriveNotes. Copyright (c) 2011, Andrei Senchuk. All rights reserved.
package net.taviscaron.drivenotes.server.util;

import net.taviscaron.drivenotes.shared.model.entity.User;
import net.taviscaron.drivenotes.shared.model.entity.Car;
import net.taviscaron.drivenotes.shared.model.entity.Place;
import net.taviscaron.drivenotes.shared.model.entity.Charging;
import net.taviscaron.drivenotes.shared.model.entity.Service;
import net.taviscaron.drivenotes.shared.model.entity.Note;
import org.hibernate.Hibernate;

/**
 * Entity utils
 * methods to creat value
 * @author Andrei Senchuk
 */
public class EntityUtils {
    public static User createUserValueObject(User obj) {
        User user = null;
        if(obj != null) {
            user = new User();
            user.setId(obj.getId());
            user.setName(obj.getName());
            user.setSurname(obj.getSurname());
            user.setUsername(obj.getUsername());
            user.setPassword(obj.getPassword());
            user.setSalt(obj.getSalt());
            user.setEmail(obj.getEmail());
            user.setAttributes(obj.getAttributes());
        }
        return user;
    }

    public static Car createCarValueObject(Car obj) {
        Car car = null;
        if(obj != null) {
            car = new Car();
            car.setId(obj.getId());
            car.setOwner(Hibernate.isPropertyInitialized(obj, "owner") ? createUserValueObject(obj.getOwner()) : null);
            car.setBrand(obj.getBrand());
            car.setModel(obj.getModel());
            car.setColor(obj.getColor());
            car.setEngineType(obj.getEngineType());
            car.setEngineVolume(obj.getEngineVolume());
            car.setYear(obj.getYear());
            car.setOdometrValue(obj.getOdometrValue());
            car.setOdometrFollowed(obj.getOdometrFollowed());
            car.setAttributes(obj.getAttributes());
        }
        return car;
    }

    public static Charging createChangringValueObject(Charging obj) {
        Charging charging = null;
        if(obj != null) {
            charging = new Charging();
            charging.setId(obj.getId());
            charging.setCar(Hibernate.isPropertyInitialized(obj, "car") ? createCarValueObject(obj.getCar()) : null);
            charging.setDate(obj.getDate());
            charging.setFillingStation(obj.getFillingStation());
            charging.setOdometrValue(obj.getOdometrValue());
            charging.setPrice(obj.getPrice());
            charging.setVolume(obj.getVolume());
        }
        return charging;
    }

    public static Service createServiceValueObject(Service obj) {
        Service service = null;
        if(obj != null) {
            service = new Service();
            service.setId(obj.getId());
            service.setCar(Hibernate.isPropertyInitialized(obj, "car") ? createCarValueObject(obj.getCar()) : null);
            service.setComment(obj.getComment());
            service.setDate(obj.getDate());
            service.setOdometrValue(obj.getOdometrValue());
            service.setProblems(obj.getProblems());
            service.setServiceCost(obj.getServiceCost());
            service.setSparesCost(obj.getSparesCost());
            service.setSpares(obj.getSpares());
        }
        return service;
    }

    public static Note createNoteValueObject(Note obj) {
        Note note = null;
        if(obj != null) {
            note = new Note();
            note.setId(obj.getId());
            note.setAuthor(Hibernate.isPropertyInitialized(obj, "author") ? createUserValueObject(obj.getAuthor()) : null);
            note.setCreateTime(obj.getCreateTime());
            note.setEditTime(obj.getEditTime());
            note.setName(obj.getName());
            note.setNote(obj.getNote());
            note.setPublic(obj.getPublic());
        }
        return note;
    }

    public static Place createPlaceValueObject(Place obj) {
        Place place = null;
        if(obj != null) {
            place = new Place();
            place.setId(obj.getId());
            place.setAuthor(Hibernate.isPropertyInitialized(obj, "author") ? createUserValueObject(obj.getAuthor()) : null);
            place.setCreateTime(obj.getCreateTime());
            place.setDescription(obj.getDescription());
            place.setLat(obj.getLat());
            place.setLon(obj.getLon());
            place.setName(obj.getName());
        }
        return place;
    }
}
